#include "serialwindow.h"
#include "ui_serialwindow.h"
#include <QDebug>
SerialWindow::SerialWindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SerialWindow)
{
    ui->setupUi(this);
   QList<QSerialPortInfo> ports = QSerialPortInfo::availablePorts();
   for(auto& port : ports)
   {
//      qDebug()<<port.portName();
       ui->portNameBox->addItem(port.portName());
   }

   sPort = new QSerialPort;

}

SerialWindow::~SerialWindow()
{
    delete ui;
}

void SerialWindow::on_openSerialPortButton_clicked()
{
    sPort->setPortName(ui->portNameBox->currentText());
    sPort->setBaudRate(9600);
    sPort->setDataBits(QSerialPort::Data8);
    sPort->setStopBits(QSerialPort::OneStop);
    sPort->setFlowControl(QSerialPort::NoFlowControl);
    sPort->setParity(QSerialPort::NoParity);

    if(sPort->open(QIODevice::ReadWrite))
    {
        ui->openSerialPortButton->setText("关闭");
    }
    else{
        QMessageBox::warning(this,"warn","open port fail");
        return;
    }

    connect(sPort,&QSerialPort::readyRead,[=](){
          ui->textEdit->append(sPort->readAll());
    });

}
